/*
 * IMonaThreadHeaderReader.java
 *
 * Created on 2007/03/27 +0900
 *
 * $Revision: 38 $
 * $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */

package org.gikolet.bbs.imona.reader;

import java.io.IOException;

import org.gikolet.bbs.data.ThreadHeader;
import org.gikolet.bbs.data.Board;
import org.gikolet.bbs.data.ThreadHeaderList;
import org.gikolet.bbs.imona.IMonaProperties;
import org.gikolet.bbs.reader.ThreadHeaderReader;
import org.gikolet.framework.util.ProgressMonitor;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 38 $ $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */
public class IMonaThreadHeaderReader extends AbstractIMonaReader implements
	ThreadHeaderReader {
    /**
     * @param properties
     */
    public IMonaThreadHeaderReader(IMonaProperties properties) {
	super(properties);
    }

    /**
     * @param properties
     * @param bufferSize
     */
    public IMonaThreadHeaderReader(IMonaProperties properties, int bufferSize) {
	super(properties, bufferSize);
    }

    public void read(Board board, ThreadHeaderList threadHeaderList, int start, int to,
	    ProgressMonitor progressMonitor) throws IOException {
	read(board, threadHeaderList, start, to, null, progressMonitor);
    }

    /**
     * @param board
     * @param threadHeaderList
     * @param start
     * @param to
     * @param find
     * @param progressMonitor
     * @throws IOException
     * @see ThreadHeaderReader
     */
    public void read(Board board, ThreadHeaderList threadHeaderList, int start, int to,
	    String find, ProgressMonitor progressMonitor) throws IOException {

	String request = "b=" + board.getNo();
	if (start != -1) {
	    request += "&c=s" + start;
	    if (to != -1) {
		request += "t" + to;
	    }
	} else {
	    start = 1;
	}
	if (find != null) {
	    request += "&w=" + find + "&m=s";
	}

	byte[] bytes = read(request, progressMonitor);

	int startByte = 0;
	int endByte;

	// 圧縮後の容量
	endByte = ReaderUtilities.indexOf(bytes, (byte) 0x09, startByte);

	// 圧縮前の容量
	startByte = endByte + 1;
	endByte = ReaderUtilities.indexOf(bytes, (byte) 0x0A, startByte);

	// スレッドの数
	startByte = endByte + 1;
	endByte = startByte + 1;
	int threadCount = ReaderUtilities.toDecimal(ReaderUtilities.getBytes(bytes,
		startByte, endByte));

	// body
	startByte = endByte;
	int i = 0;
	while (i < threadCount) {
	    endByte = ReaderUtilities.indexOf(bytes, (byte) 0x0A, startByte);
	    if (endByte == -1) {
		endByte = bytes.length;
	    }
	    // スレッド番号
	    int s = startByte;
	    int e = ReaderUtilities.indexOf(bytes, (byte) 0x09, startByte);
	    int threadNo = ReaderUtilities.toDecimal(ReaderUtilities
		    .getBytes(bytes, s, e));

	    // スレッドタイトル
	    s = e + 1;
	    e = ReaderUtilities.indexOf(bytes, (byte) 0x09, s);
	    String threadSubject = ReaderUtilities.toString(bytes, s, e - s);

	    // レスの数
	    s = e + 1;
	    e = endByte;
	    int resCount = ReaderUtilities.toDecimal(ReaderUtilities
		    .getBytes(bytes, s, e));

	    threadHeaderList.set(new ThreadHeader(board, threadNo, threadSubject,
		    resCount), start + i);

	    startByte = endByte + 1;
	    i++;
	}
    }
}
